//
//  CYNewsViewController.m
//  cyjf
//
//  Created by tauCross on 2017/7/6.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYNewsViewController.h"

#import "CYEmptyCell.h"
#import "CYNewsCell.h"

#import "CYDataAPINewsModel.h"

#import "CYWebViewController.h"



@interface CYNewsViewController ()

@property(nonatomic, assign)NSInteger page_no;
@property(nonatomic, assign)BOOL loading;
@property(nonatomic, assign)BOOL hasMore;

@property(nonatomic, assign)BOOL errorShowed;

@end


@implementation CYNewsViewController

- (void)loadUI
{
    self.navigationItem.title = @"车蚁头条";
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
}

- (void)loadData
{
    self.page_no = 1;
    self.loading = NO;
    self.hasMore = YES;
    
    self.tableModel = [[NIMutableTableViewModel alloc] initWithSectionedArray:@[] delegate:(id)[NICellFactory class]];
    self.tableView.dataSource = self.tableModel;
    [self.tableView reloadData];
    
    [self loadDataFromServer];
}

- (void)loadDataFromServer
{
    if(self.loading)
    {
        return;
    }
    if(self.hasMore == NO)
    {
        return;
    }
    self.loading = YES;
    @weakify(self);
    [CYStandardDataAPI news_getListWithPage_no:@(self.page_no) complete:^(CYResponse *response) {
        @strongify(self);
        if(response.code == 0)
        {
            self.hasMore = NO;
            NSMutableArray *cells = [NSMutableArray array];
            if([response.content isKindOfClass:[NSArray class]])
            {
                self.hasMore = [response.content count] >= CY_API_PAGE_SIZE;
                self.page_no ++;
                NSArray *models = [CYDataAPINewsModel mj_objectArrayWithKeyValuesArray:response.content];
                for(CYDataAPINewsModel *model in models)
                {
                    {
                        CYEmptyCellObject *cell = [CYEmptyCellObject objectWithCellClass:[CYEmptyCell class]];
                        cell.height = 10;
                        [cells addObject:cell];
                    }
                    {
                        CYNewsCellObject *cell = [CYNewsCellObject objectWithCellClass:[CYNewsCell class]];
                        cell.title = SAFE_STRING(model.title);
                        cell.source = SAFE_STRING(model.source);
                        cell.thumb = SAFE_STRING(model.smallImg);
                        cell.date = [[SAFE_NUMBER(model.publishTime) cy_date] cy_stringWithFormat:@"yyyy-MM-dd"];
                        cell.id = SAFE_NUMBER(model.id);
                        [cells addObject:cell];
                    }
                }
            }
            [self.tableModel addObjectsFromArray:cells];
            [self.tableView reloadData];
            self.loading = NO;
        }
        else
        {
            if(self.errorShowed == NO)
            {
                [SVProgressHUD showErrorWithStatus:response.message];
                self.errorShowed = YES;
            }
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                @strongify(self);
                self.loading = NO;
            });
        }
    }];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(scrollView == self.tableView)
    {
        if(scrollView.contentSize.height - scrollView.contentOffset.y < scrollView.height * 2)
        {
            [self loadDataFromServer];
        }
    }
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CYNewsCellObject *cell = [self.tableModel objectAtIndexPath:indexPath];
    if([cell isKindOfClass:[CYNewsCellObject class]])
    {
        CYWebViewController *webViewController = [[CYWebViewController alloc] init];
        webViewController.url = [CY_H5_BASE_URL stringByAppendingFormat:@"views/news.html?id=%@", cell.id];
        [self.navigationController pushViewController:webViewController animated:YES];
    }
}

@end
